package com.gph.saviorframework.os.authz.web.controller.clientdetails;

import com.gph.saviorframework.Constants;
import com.gph.saviorframework.SaviorFrameworkException;
import com.gph.saviorframework.os.authz.service.ClientDetailsService;
import com.gph.saviorframework.os.model.oauth.ClientDetails;
import com.gph.saviorframework.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * rest接口控制器
 */
@RestController
@RequestMapping("/"+ Constants.FRAMEWORK_REQUEST_PATH+"/client/api")
public class ClientDetailsRestController {

    @Autowired
    private ClientDetailsService clientDetailsService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap create(@ModelAttribute ClientDetails clientDetails) throws SaviorFrameworkException{
        ModelMap modelMap = new ModelMap();
        try {

            clientDetailsService.save(clientDetails);
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.TRUE);
        }
        catch (Exception ex){
            ex.printStackTrace();
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY, Boolean.FALSE);
            modelMap.addAttribute(Constants.ERROR_INFO, ex.getMessage());
        }

        return modelMap;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public String list(
            @RequestParam(value = Constants.DEFAULT_PAGE_MODEL_KEY, required = false, defaultValue = Constants.DEFAULT_PAGE_START) Integer start,
            @RequestParam(value = Constants.DEFAULT_RECORD_MODEL_KEY, required = false, defaultValue = Constants.DEFAULT_PAGE_SIZE) Integer limit,
            @RequestParam(value = Constants.DEFAULT_SORT_MODEL_KEY, required = false, defaultValue = Constants.DEFAULT_SORT_FIELD) String sort,
            @RequestParam(value = Constants.DEFAULT_DIR_MODEL_KEY, required = false, defaultValue = Constants.DEFAULT_SORT_DIRECTION) String dir,
            HttpServletRequest request) throws SaviorFrameworkException {
        String json = null;
        Map<String, Object> params = RequestUtil.getQueryMap(request, clientDetailsService.getFields());
        try {
            json = com.alibaba.fastjson.JSONArray.toJSONString(clientDetailsService.find(start,limit,sort,dir,params));
        }
        catch (DataAccessException e) {
            e.printStackTrace();
        }
        return json;
    }

    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ModelMap get(@PathVariable("id") String id) throws SaviorFrameworkException {
        ModelMap modelMap = new ModelMap();
        try {
            List<ClientDetails> clientDetailses=clientDetailsService.findClientDetails(id);
            ClientDetails clientDetails = clientDetailses.size()>0?clientDetailses.get(0):null;
            modelMap.addAttribute(Constants.DEFAULT_RECORD_MODEL_KEY, clientDetails);
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);
        }
        catch (Exception e) {
            e.printStackTrace();
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY, Boolean.FALSE);
            modelMap.addAttribute(Constants.DEFAULT_ERROR_KEY, e.getMessage());
        }
        return modelMap;
    }
}
